package com.xxg.renrenorder.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * RabbitMQ配置
 *
 * @author xxg
 * @date 2020/12/29
 */
@Configuration
public class RabbitMQConfig {
    public static final String DELAY_EXCHANGE_NAME = "rrsc.delay.order.exchange";
    /**
     * 订单30分钟超时支付延时队列
     */
    public static final String DELAY_QUEUE_NAME = "rrsc.delay.order.queue";
    /**
     * 拼团30分钟超时设置默认延时队列
     */
    public static final String DELAY_QUEUE_NAME_GROUP_BUY = "rrsc.delay.order.queue.group.buy";
    /**
     * 自动确认收货延时队列10天
     */
    public static final String DELAY_QUEUEA_NAME = "rrsc.delay.order.queuea";
    /**
     * 收货后15天售后服务器延时队列
     */
    public static final String DELAY_QUEUEB_NAME = "rrsc.delay.order.queueb";
    /**
     * 收货后30天售后服务器延时队列
     */
    public static final String DELAY_QUEUEC_NAME = "rrsc.delay.order.queuec";
    public static final String DELAY_QUEUE_ROUTING_KEY = "rrsc.delay.order.queue.routingkey";
    public static final String DELAY_QUEUE_GROUP_BUY_ROUTING_KEY = "rrsc.delay.order.queue.group.buy.routingkey";
    public static final String DELAY_QUEUEA_ROUTING_KEY = "rrsc.delay.order.queuea.routingkey";
    public static final String DELAY_QUEUEB_ROUTING_KEY = "rrsc.delay.order.queueb.routingkey";
    public static final String DELAY_QUEUEC_ROUTING_KEY = "rrsc.delay.order.queuec.routingkey";
    public static final String DEAD_LETTER_EXCHANGE = "rrsc.delay.order.dl.exchange";
    public static final String DEAD_LETTER_QUEUE_ROUTING_KEY = "rrsc.delay.order.dl.delay_30m.routingkey";
    public static final String DEAD_LETTER_QUEUE_GROUP_BUY_ROUTING_KEY = "rrsc.delay.order.dl.group.buy.delay_30m.routingkey";
    public static final String DEAD_LETTER_QUEUEA_ROUTING_KEY = "rrsc.delay.order.dl.delay_10d.routingkey";
    public static final String DEAD_LETTER_QUEUEB_ROUTING_KEY = "rrsc.delay.order.dl.delay_15d.routingkey";
    public static final String DEAD_LETTER_QUEUEC_ROUTING_KEY = "rrsc.delay.order.dl.delay_30d.routingkey";
    public static final String DEAD_LETTER_QUEUE_NAME = "rrsc.delay.order.dl.queue";
    public static final String DEAD_LETTER_QUEUE_NAME_GROUP_BUY = "rrsc.delay.order.dl.queue.group.buy";
    public static final String DEAD_LETTER_QUEUEA_NAME = "rrsc.delay.order.dl.queuea";
    public static final String DEAD_LETTER_QUEUEB_NAME = "rrsc.delay.order.dl.queueb";
    public static final String DEAD_LETTER_QUEUEC_NAME = "rrsc.delay.order.dl.queuec";

    /**
     * 声明延时Exchange
     */
    @Bean("delayExchange")
    public DirectExchange delayExchange() {
        return new DirectExchange(DELAY_EXCHANGE_NAME);
    }

    /**
     * 声明死信Exchange
     */
    @Bean("deadLetterExchange")
    public DirectExchange deadLetterExchange() {
        return new DirectExchange(DEAD_LETTER_EXCHANGE);
    }

    /**
     * 声明延时队列 延时30m,并绑定到对应的死信交换机
     */
    @Bean("delayQueue")
    public Queue delayQueue() {
        Map<String, Object> args = new HashMap<>(5);
        // x-dead-letter-exchange    这里声明当前队列绑定的死信交换机
        args.put("x-dead-letter-exchange", DEAD_LETTER_EXCHANGE);
        // x-dead-letter-routing-key  这里声明当前队列的死信路由key
        args.put("x-dead-letter-routing-key", DEAD_LETTER_QUEUE_ROUTING_KEY);
        // x-message-ttl  声明队列的TTL
        args.put("x-message-ttl", 1800000);
        return QueueBuilder.durable(DELAY_QUEUE_NAME).withArguments(args).build();
    }

    /**
     * 声明延时队列 延时30m,并绑定到对应的死信交换机
     */
    @Bean("delayQueueGroupBuy")
    public Queue delayQueueGroupBuy() {
        Map<String, Object> args = new HashMap<>(5);
        // x-dead-letter-exchange    这里声明当前队列绑定的死信交换机
        args.put("x-dead-letter-exchange", DEAD_LETTER_EXCHANGE);
        // x-dead-letter-routing-key  这里声明当前队列的死信路由key
        args.put("x-dead-letter-routing-key", DEAD_LETTER_QUEUE_GROUP_BUY_ROUTING_KEY);
        // x-message-ttl  声明队列的TTL
        args.put("x-message-ttl", 1800000);
        return QueueBuilder.durable(DELAY_QUEUE_NAME_GROUP_BUY).withArguments(args).build();
    }

    /**
     * 声明延时队列A 延时10天,并绑定到对应的死信交换机
     */
    @Bean("delayQueueA")
    public Queue delayQueueA() {
        Map<String, Object> args = new HashMap<>(5);
        // x-dead-letter-exchange    这里声明当前队列绑定的死信交换机
        args.put("x-dead-letter-exchange", DEAD_LETTER_EXCHANGE);
        // x-dead-letter-routing-key  这里声明当前队列的死信路由key
        args.put("x-dead-letter-routing-key", DEAD_LETTER_QUEUEA_ROUTING_KEY);
        // x-message-ttl  声明队列的TTL
        args.put("x-message-ttl", 864000000);
        return QueueBuilder.durable(DELAY_QUEUEA_NAME).withArguments(args).build();
    }

    /**
     * 声明延时队列B 延时15天,并绑定到对应的死信交换机
     */
    @Bean("delayQueueB")
    public Queue delayQueueB() {
        Map<String, Object> args = new HashMap<>(5);
        // x-dead-letter-exchange    这里声明当前队列绑定的死信交换机
        args.put("x-dead-letter-exchange", DEAD_LETTER_EXCHANGE);
        // x-dead-letter-routing-key  这里声明当前队列的死信路由key
        args.put("x-dead-letter-routing-key", DEAD_LETTER_QUEUEB_ROUTING_KEY);
        // x-message-ttl  声明队列的TTL
        args.put("x-message-ttl", 1296000000);
        return QueueBuilder.durable(DELAY_QUEUEB_NAME).withArguments(args).build();
    }

    /**
     * 声明延时队列C 延时15天,并绑定到对应的死信交换机
     */
    @Bean("delayQueueC")
    public Queue delayQueueC() {
        Map<String, Object> args = new HashMap<>(5);
        // x-dead-letter-exchange    这里声明当前队列绑定的死信交换机
        args.put("x-dead-letter-exchange", DEAD_LETTER_EXCHANGE);
        // x-dead-letter-routing-key  这里声明当前队列的死信路由key
        args.put("x-dead-letter-routing-key", DEAD_LETTER_QUEUEC_ROUTING_KEY);
        // x-message-ttl  声明队列的TTL
        args.put("x-message-ttl", 1296000000L);
        return QueueBuilder.durable(DELAY_QUEUEC_NAME).withArguments(args).build();
    }

    /**
     * 声明死信队列 用于接收延时30m处理的消息
     */
    @Bean("deadLetterQueue")
    public Queue deadLetterQueue() {
        return new Queue(DEAD_LETTER_QUEUE_NAME);
    }

    /**
     * 声明死信队列 用于接收延时30m处理的消息
     */
    @Bean("deadLetterQueueGroupBuy")
    public Queue deadLetterQueueGroupBuy() {
        return new Queue(DEAD_LETTER_QUEUE_NAME_GROUP_BUY);
    }

    /**
     * 声明死信队列 用于接收延时10天处理的消息
     */
    @Bean("deadLetterQueueA")
    public Queue deadLetterQueueA() {
        return new Queue(DEAD_LETTER_QUEUEA_NAME);
    }

    /**
     * 声明死信队列 用于接收延时15天处理的消息
     */
    @Bean("deadLetterQueueB")
    public Queue deadLetterQueueB() {
        return new Queue(DEAD_LETTER_QUEUEB_NAME);
    }

    /**
     * 声明死信队列 用于接收延时30天处理的消息
     */
    @Bean("deadLetterQueueC")
    public Queue deadLetterQueueC() {
        return new Queue(DEAD_LETTER_QUEUEC_NAME);
    }

    /**
     * 声明延时队列绑定关系
     */
    @Bean
    public Binding delayBinding(@Qualifier("delayQueue") Queue queue,
                                @Qualifier("delayExchange") DirectExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(DELAY_QUEUE_ROUTING_KEY);
    }

    /**
     * 声明延时队列绑定关系
     */
    @Bean
    public Binding delayBindingGroupBuy(@Qualifier("delayQueueGroupBuy") Queue queue,
                                        @Qualifier("delayExchange") DirectExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(DELAY_QUEUE_GROUP_BUY_ROUTING_KEY);
    }

    /**
     * 声明延时队列绑定关系
     */
    @Bean
    public Binding delayBindingA(@Qualifier("delayQueueA") Queue queue,
                                 @Qualifier("delayExchange") DirectExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(DELAY_QUEUEA_ROUTING_KEY);
    }

    /**
     * 声明延时队列绑定关系
     */
    @Bean
    public Binding delayBindingB(@Qualifier("delayQueueB") Queue queue,
                                 @Qualifier("delayExchange") DirectExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(DELAY_QUEUEB_ROUTING_KEY);
    }

    /**
     * 声明延时队列绑定关系
     */
    @Bean
    public Binding delayBindingC(@Qualifier("delayQueueC") Queue queue,
                                 @Qualifier("delayExchange") DirectExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(DELAY_QUEUEC_ROUTING_KEY);
    }

    /**
     * 声明死信队列绑定关系
     */
    @Bean
    public Binding deadLetterBinding(@Qualifier("deadLetterQueue") Queue queue,
                                     @Qualifier("deadLetterExchange") DirectExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(DEAD_LETTER_QUEUE_ROUTING_KEY);
    }

    /**
     * 声明死信队列绑定关系
     */
    @Bean
    public Binding deadLetterBindingGroupBuy(@Qualifier("deadLetterQueueGroupBuy") Queue queue,
                                             @Qualifier("deadLetterExchange") DirectExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(DEAD_LETTER_QUEUE_GROUP_BUY_ROUTING_KEY);
    }

    /**
     * 声明死信队列绑定关系
     */
    @Bean
    public Binding deadLetterBindingA(@Qualifier("deadLetterQueueA") Queue queue,
                                      @Qualifier("deadLetterExchange") DirectExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(DEAD_LETTER_QUEUEA_ROUTING_KEY);
    }

    /**
     * 声明死信队列绑定关系
     */
    @Bean
    public Binding deadLetterBindingB(@Qualifier("deadLetterQueueB") Queue queue,
                                      @Qualifier("deadLetterExchange") DirectExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(DEAD_LETTER_QUEUEB_ROUTING_KEY);
    }

    /**
     * 声明死信队列绑定关系
     */
    @Bean
    public Binding deadLetterBindingC(@Qualifier("deadLetterQueueC") Queue queue,
                                      @Qualifier("deadLetterExchange") DirectExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(DEAD_LETTER_QUEUEC_ROUTING_KEY);
    }
}
